package com.d2.modules.system.dict.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.d2.common.domain.Result;
import com.d2.modules.system.dict.entity.Dict;
import com.d2.modules.system.dict.entity.DictItem;
import com.d2.modules.system.dict.service.DictItemService;
import com.d2.modules.system.dict.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("dict")
public class DictController {
    @Autowired
    private DictItemService dictItemService;

    @Autowired
    private DictService dictService;

    @GetMapping("page")
    public Result<Page<Dict>> list(Page<Dict> page, Dict query){
        return Result.success(dictService.page(page,new QueryWrapper<>(query)));
    }

    @PostMapping("add")
    public Result<Void> add(@RequestBody Dict obj){
        dictService.save(obj);
        return Result.success();
    }

    @PutMapping("update")
    public Result<Void> update(@RequestBody Dict obj){
        dictService.updateById(obj);
        return Result.success();
    }

    @DeleteMapping("delete/{id}")
    @Transactional
    public Result<Void> delete(@PathVariable Long id){
        dictService.removeById(id);
        DictItem query = new DictItem();
        query.setDictId(id);
        dictItemService.remove(new QueryWrapper<>(query));
        return Result.success();
    }
}
